package com.uvnos4j.matilda.security.controller;

import com.uvnos4j.matilda.commons.verification.Verification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.OutputStream;

/**
 * 验证码 Controller
 * <p>
 * Description:验证码 Controller
 * </p>
 *
 * @author Guo.wl
 * @version v1.0.0
 * @see com.uvnos4j.matilda.security.controller
 * @since 2020-08-31 22:30:47
 */
@Controller
public class CaptchaController {

    @GetMapping("/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
        /* 随机生成字符串，并写入session */
        String code = Verification.getRandCode(4);
        request.getSession().setAttribute("VerificationCode", code);

        BufferedImage image = Verification.getImage(130, 44, code);
        response.setContentType("image/png");
        OutputStream out = response.getOutputStream();
        out.write(Verification.getByteArray(image));
        out.flush();
        out.close();
    }
}
